package com.webservice.dao.factory.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.webservice.dao.factory.IntoStoreDao;
import com.webservice.utils.UUIDUtil;
import com.webservice.vo.factory.IntoStockInLogVo;
import com.webservice.vo.factory.IntoStoreAggVO;


@Repository("IntoStoreDao")
public class IntoStoreDaoImpl implements IntoStoreDao {
	private static Logger logger = Logger.getLogger(IntoStoreDaoImpl.class);
	
	
	@Autowired
	@Qualifier("datatemplate")
	private JdbcTemplate jdbcTemplate;

	//把意向入库单明细进行记录（WMS->B2B）
	@Override
	public boolean insertWmsPurposeStockinLog(IntoStoreAggVO svo) {
		// TODO Auto-generated method stub
		String sql = "INSERT INTO WMS_PURPOSE_STOCKIN_LOG(ID,PURPOSE_STOCKIN_CODE,PURPOSE_STOCKIN_DETAIL_CODE,QUANTITY,PRODUCTID,STOCKIN_TIME) VALUES(?,?,?,?,?,?)";
		List<Object[]> batchArgs = new ArrayList<Object[]>();
		for (int i=0;i<svo.getProducts().size();i++) {
			Object[] args = new Object[6];
			args[0] = UUIDUtil.getUUID();
			args[1] = svo.getStokcincode();
			args[2] = svo.getProducts().get(i).getProductcode();
			args[3] = svo.getProducts().get(i).getQuantity();
			args[4] = svo.getProducts().get(i).getProductcode();
			args[5] = svo.getIntodate();
			batchArgs.add(args);
		}
		int[] count = jdbcTemplate.batchUpdate(sql, batchArgs);
		if(svo.getProducts().size()==count.length){
			return true;
		}
		return false;
	}

	//查询意向入库明细单的意向入库明细编号
	@Override
	public List<IntoStockInLogVo> getPurposeStockDetailCode(IntoStoreAggVO svo) {
		List<IntoStockInLogVo>  stockDetailCodelist=new ArrayList<IntoStockInLogVo>();
		StringBuilder sbSql = new StringBuilder();
		sbSql.append("SELECT WPS.PURPOSE_STOCKIN_CODE,WSD.PURPOSE_STOCKIN_DETAIL_CODE,WSD.PRODUCTID,WSD.ACTUAL_QUANTITY,WPS.STOCKIN_TIME FROM WMS_PURPOSE_STOCKIN WPS");
		sbSql.append(" INNER JOIN WMS_PURPOSE_STOCKIN_DETAIL WSD ON WSD.PURPOSE_STOCKIN_CODE=WPS.PURPOSE_STOCKIN_CODE");
		sbSql.append(" WHERE WPS.PURPOSE_STOCKIN_CODE=?");
		String sql=sbSql.toString();
		Object[] parms = new Object[]{
				svo.getStokcincode()
				};
		List<IntoStockInLogVo> list =  (List<IntoStockInLogVo>)jdbcTemplate.query(sql, parms, new RowMapper<IntoStockInLogVo>(){
			public IntoStockInLogVo mapRow(ResultSet rs,int i) throws SQLException{
				IntoStockInLogVo vo=new IntoStockInLogVo();
			    vo.setStockInCode(rs.getString("PURPOSE_STOCKIN_CODE"));
			    vo.setStockDetailCode(rs.getString("PURPOSE_STOCKIN_DETAIL_CODE"));
			    vo.setProductcode(rs.getString("PRODUCTID"));
			    vo.setQuantity(rs.getString("ACTUAL_QUANTITY"));
			    vo.setStocktime(rs.getString("STOCKIN_TIME"));
				return vo;
			}
		});
		if(list!=null&&list.size()>0){
			stockDetailCodelist=list;
		}
		return stockDetailCodelist;
	}

	//记录意向入库单产品实际入库数量和入库时间
	@Override
	public boolean updatePurposeStock(IntoStoreAggVO svo) {
		// TODO Auto-generated method stub
		String sql="UPDATE WMS_PURPOSE_STOCKIN WPS SET WPS.STOCKIN_TIME=TO_DATE(?,'yyyy-mm-dd hh24:mi:ss')  WHERE WPS.PURPOSE_STOCKIN_CODE=? and STOCKIN_TIME is null";
		String sql2="UPDATE WMS_PURPOSE_STOCKIN_DETAIL WSD  SET WSD.ACTUAL_QUANTITY= decode(ACTUAL_QUANTITY,'','0',ACTUAL_QUANTITY) + ?  WHERE WSD.PRODUCTID=?  AND WSD.PURPOSE_STOCKIN_CODE=?";
		Object[] parms={svo.getIntodate(),svo.getStokcincode()};
		int i=jdbcTemplate.update(sql, parms);
			for(int j=0;j<svo.getProducts().size();j++){
				Object[] parms1={svo.getProducts().get(j).getQuantity(),svo.getProducts().get(j).getProductcode(),svo.getStokcincode()};
				jdbcTemplate.update(sql2, parms1);
			}
		return true;
	}
	

}
